﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;
namespace SachOnlineLibrary.DAL
{
    internal class KetNoiDB
    {
        SqlConnection conn = new SqlConnection();
        private void MoKetNoi()
        {
            if (conn.State == ConnectionState.Closed)
            {
                try
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["ChuoiKetNoi"].ToString();
                    conn.Open();
                }
                catch (SqlException ex)
                {
                    Console.Write(ex.Message);
                }
            }
        }
        private void DongKetNoi()
        {
            if (conn.State != ConnectionState.Closed)
            {
                try 
                {
                    conn.Close();
                    conn.Dispose();
                }
                catch (SqlException ex)
                {
                    Console.Write(ex.Message);
                }
            }
        }
        internal DataTable ExecuteQuery(SqlCommand cmd)
        {
            cmd.Connection = conn;
            MoKetNoi();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return null;
            }
            finally 
            {
                DongKetNoi();
            }
        }
        internal bool ExecuteNonQuery(SqlCommand cmd)
        {
            cmd.Connection = conn;
            try
            {
                MoKetNoi();
                if (cmd.ExecuteNonQuery() > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
            finally
            {
                DongKetNoi();
            }
        }
        public bool ExecuteReader(SqlCommand cmd)
        {
            bool kt = false;
            try
            {
                MoKetNoi();
                cmd.Connection = conn;
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                    kt = true;
            }
            catch (Exception e)
            {

            }
            finally
            {
                DongKetNoi();
            }
            return kt;
        }
        internal string ExecuteScalar(SqlCommand cmd)
        {
            cmd.Connection = conn;
            try
            {
                MoKetNoi();
                return cmd.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return null;
            }
            finally
            {
                DongKetNoi();
            }
        }

    }
}
